package top.mingtea.propertymannagement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import top.mingtea.propertymannagement.controller.*;
import top.mingtea.propertymannagement.pojo.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;

@Component
public class Menu {
    private final AdministratorController administratorController;
    private final BuildingController buildingController;
    private final HouseController houseController;
    private final property_feeController property_feeController;
    private final property_managementController propertymanagementController;
    private final proprietorController proprietorcontroller;
    private proprietor currentpropritor;

    @Autowired
    public Menu(AdministratorController administratorController,BuildingController buildingController,HouseController houseController,property_feeController property_feeController,property_managementController propertymanagementController,proprietorController proprietorcontroller) {
        this.administratorController = administratorController;
        this.houseController=houseController;
        this.buildingController=buildingController;
        this.property_feeController=property_feeController;
        this.propertymanagementController=propertymanagementController;
        this.proprietorcontroller=proprietorcontroller;
    }

    public void login() {
        System.out.println("欢迎来到小区管理系统,请输入“管理员登录”或“业主登录”或输入其它任意字符退出");
        Scanner scanner=new Scanner(System.in);
        String command = scanner.nextLine();
        if ("管理员".equalsIgnoreCase(command)) {
            System.out.print("用户: ");
            String username = scanner.nextLine();
            System.out.print("密码: ");
            String password = scanner.nextLine();
            boolean response = administratorController.login(username,password);

            // 打印结果
            System.out.println("Response: " + response);

            if(true){
                this.displayMenuForAdmin();
            }
            else {
                System.out.println("用户名/密码错误");
            }
        }else if ("业主".equalsIgnoreCase(command)){
            System.out.print("用户: ");
            String username = scanner.nextLine();
            System.out.print("密码: ");
            String password = scanner.nextLine();
            boolean isAuthenticated = proprietorcontroller.login(username, password);
            if(isAuthenticated){
                currentpropritor=proprietorcontroller.returnCurrentuser(username);
                this.displayMenuForProprietor();
            }
            else {
                System.out.println("用户名/密码错误");
            }
        }
        else {
            System.out.println("程序退出...");
        }
        scanner.close();
    }
    public void displayMenuForAdmin(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("登陆成功,欢迎管理员访问");
        System.out.println("1.查询与调整楼宇详况");
        System.out.println("2.查询与调整房屋详况");
        System.out.println("3.查询与调整业主详况");
        System.out.println("4.查询与调整物管需求详况");
        System.out.println("5.查询与调整物业费详况");
        System.out.println("6.查询（实际仅测试用,并不是程序该有的功能）");
        System.out.println("7.处理事件");
        String Option=scanner.nextLine();
        switch (Option){
            case "6" -> this.query();
            case "7" -> this.handleManagementOfAdmin();
        }
    }
    public void displayMenuForProprietor() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("登陆成功,欢迎尊贵的业主"+currentpropritor.getProprietor_name()+"访问");
        System.out.println("1.物管需求申请与进度查看");
        System.out.println("2.缴纳物业费");
        String Option = scanner.nextLine();
        switch (Option) {
            case "1" -> this.handleManagementOfProprietor();
            case "2" -> this.handleManagementOfProprietor();
        }
    }
    public void displayMenuForSuperAdmin() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("登陆成功,欢迎超级管理员访问");
        System.out.println("1.查询与调整楼宇详况");
        System.out.println("2.查询与调整房屋详况");
        System.out.println("3.查询与调整业主详况");
        System.out.println("4.查询与调整物管需求详况");
        System.out.println("5.查询与调整物业费详况");
        System.out.println("6.查询与调整下位管理员详况");
        String Option = scanner.nextLine();
        switch (Option) {
            case "1" -> this.query();
            case "2" -> this.handleManagementOfProprietor();
        }
    }
    public void query(){
        System.out.println("请问你想要查询:");
        System.out.println("1.所有楼宇的明细信息:");
        System.out.println("2.所有房屋的明细信息");
        System.out.println("3.所有业主信息");
        System.out.println("4.物业管理情况");
        System.out.println("5.各物业费缴纳情况");
        Scanner scanner=new Scanner(System.in);
        while (true) {
            String Option=scanner.nextLine();
            switch (Option) {
                case "1" -> buildingController.printAllBuildings();
                case "2" -> houseController.printAllHouses();
                case "3" -> proprietorcontroller.printAllProprietors();
                case "4" -> propertymanagementController.printAllPropertyManagements();
                case "5" -> property_feeController.printAllPropertyFees();
                default -> System.out.println("无效选项，请重新输入。");
            }
        }
    } //实际仅测试用,并不是程序该有的功能
    public void handleManagementOfAdmin(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("1.查看未处理事件列表");
        System.out.println("2.查看处理中事件列表");
        System.out.println("3.查看已处理事件列表");
        String Option=scanner.nextLine();
        switch (Option){
            case "1" -> printEvents(propertymanagementController.getUnprocessedEvents());
            case "2" -> printEvents(propertymanagementController.getProcessingEvents());
            case "3" -> printEvents(propertymanagementController.getProcessedEvents());
        }
    }
    public void handleManagementOfProprietor(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("1.登记物管需求事件");
        System.out.println("2.查看已登记的物管需求事件");
     String Option=scanner.nextLine();
      switch (Option){
       case "1" -> this.registerManagementEvent();
          case "2" -> this.query();
      }
    }
    private void registerManagementEvent() {
        Scanner scanner = new Scanner(System.in);
        property_management propertyManagement = new property_management();

        propertyManagement.setProprietor_id(currentpropritor.getProprietor_id());

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedNow = now.format(formatter);
        propertyManagement.setManagement_date(formattedNow);

        System.out.print("请输入请求内容: ");
        propertyManagement.setRequest(scanner.nextLine());

        propertyManagement.setHandling_status("未处理");

        propertyManagement.setSatisfaction("待评价");

        propertyManagement.setResponsible_person("暂无受理负责人");
        propertymanagementController.save(propertyManagement);

        System.out.println("物管需求事件已登记，我们将尽快安排人手处理");
    }
    public void payFeesForProprietor(){
        System.out.println("您旗下共？套房产，单月将产生？元物业费，请输入您想要缴纳的月份数：");
    }


    public void AdjustBuildingForAdmin(){
        System.out.println("请输入id查询指定条目进行编辑");
    }
    public void AdjustHouseForAdmin(){
        System.out.println("请输入id查询指定条目进行编辑");
    }
    public void AdjustAdminForSuperAdmin(){
        System.out.println("请输入id查询指定条目进行编辑");
    }
    public void AdjustpropertyProprietorForAdmin(){
        System.out.println("请输入id查询指定条目进行编辑");
    }
    private void printEvents(List<property_management> managements) {
        if (managements.isEmpty()) {
            System.out.println("没有找到相关事件。");
            return;
        }

        System.out.println("业主ID\t业主姓名\t管理日期\t请求内容\t处理状态\t满意度\t负责人");
        System.out.println("==================================================================================");
        for (property_management event : managements) {
            System.out.printf("%s\t%s\t%s\t%s\t%s\t%s%n",
                    event.getProprietor_id(),
                    proprietorcontroller.returnProprietorbyId(event.getProprietor_id()).getProprietor_name(),
                    event.getManagement_date(),
                    event.getRequest(),
                    event.getHandling_status(),
                    event.getSatisfaction(),
                    event.getResponsible_person());
        }
        if(managements.get(0).getProprietor_id().equals("暂无受理负责人")){
            System.out.println("存在未受理事件，您可以通过输入业主ID+请求内容");
        }

    }

}